//
//  CommonFunc.m
//  BarCode
//
//  Created by Dale on 17/4/5.
//  Copyright © 2017年 MobileArtisan. All rights reserved.
//

#import "CommonFunc.h"
#import <BmobSDK/Bmob.h>
#import "Billing-Swift.h"

@implementation CommonFunc

//字符串为空检查
+ (BOOL)isEmptyString:(NSString *)sourceStr {
    if ((NSNull *)sourceStr == [NSNull null]) {
        return YES;
    }
    if (sourceStr == nil) {
        return YES;
    }
    if (sourceStr == NULL) {
        return YES;
    }
    if ([sourceStr isEqual:[NSNull null]]) {
        return YES;
    }
    if (![sourceStr isKindOfClass:[NSString class]]) {
        return YES;
    }
    if([sourceStr isEqualToString:@"<null>"]){
        return YES;
    }
    if ([sourceStr isEqualToString:@"null"]) {
        return YES;
    }
    if ([sourceStr isEqualToString:@""]) {
        return YES;
    }
    if (sourceStr.length == 0) {
        return YES;
    }
    return NO;
}


//MARK:- request network
+ (void)getUsersWithResult:(HandleBlock)block {
    BmobQuery *bquery = [BmobQuery queryWithClassName:@"person"];
    bquery.limit = 1000;
    [bquery findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error) {
        if(error == nil) {
            NSLog(@"get users from network:%@", array);
            [Bridge2OC removeUserList];
            for (BmobObject *result in array) {
                User *tmpUser = [[User alloc] init];
                NSDictionary *dic = @{
                                      @"username":[result objectForKey:@"username"],
                                      @"objectId":result.objectId,
                                      @"full_name":[result objectForKey:@"full_name"],
                                      @"role":[result objectForKey:@"role"],
                                      @"staff_code":[result objectForKey:@"staff_code"]?[result objectForKey:@"staff_code"]:@""
                                      };
                [tmpUser transeferDic2UserWithDic:dic];
                [Bridge2OC appendUserListWithUser:tmpUser];
            }
            block([Bridge2OC getUserList], 1);
        } else {
            block(error.localizedDescription, 0);
        }
    }];
}

+ (void)bind:(NSString *)userName FullName:(NSString *)fullName Code:(NSString *)code GoodsName:(NSString *)goodsName Block:(HandleBlock)block {
    //绑定接口
    BmobObject *user2codeObj = [BmobObject objectWithClassName:@"user2code"];
    [user2codeObj setObject:userName forKey:@"user_id"];
    [user2codeObj setObject:fullName forKey:@"full_name"];
    [user2codeObj setObject:code forKey:@"code"];
    [user2codeObj setObject:goodsName forKey:@"goods_name"];
    [user2codeObj saveInBackgroundWithResultBlock:^(BOOL isSuccessful, NSError *error) {
        if(error == nil) {
            if (isSuccessful) {
                block(@"绑定成功", 1);
            } else {
                block(@"绑定失败", 0);
            }
        } else {
            block(error.localizedDescription, 0);
        }
    }];
}


+ (void)findGoodsName:(NSString *)code Block:(HandleBlock)block {
    //绑定接口
    BmobQuery *bquery = [BmobQuery queryWithClassName:@"goods"];
    [bquery whereKey:@"goods_code" equalTo:code];
    [bquery findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error) {
        if (error != nil) {
            block(error.localizedDescription, 0);
        } else {
            block([((BmobObject *)array.firstObject) objectForKey:@"goods_name"], 1);
        }
    }];
}

+ (void)modifyPasswordWithOldPassword:(NSString *)oldPassword newPassword:(NSString *)newPassword block:(HandleBlock)block {
    BmobUser *user = [BmobUser currentUser];
    [user updateCurrentUserPasswordWithOldPassword:oldPassword newPassword:newPassword block:^(BOOL isSuccessful, NSError *error) {
        if (isSuccessful) {
            block(@"修改成功，请重新登录", 1);
            //用新密码登录
//            [BmobUser loginInbackgroundWithAccount:user.username andPassword:newPassword block:^(BmobUser *user, NSError *error) {
//                if (error) {
//                    block(error.localizedDescription, 0);
//                } else {
//                    block(@"修改成功，请重新登录", 1);
//                }
//            }];
        } else {
            NSLog(@"change password error:%@",error);
            block(error.localizedDescription, 0);
        }
    }];
}

+ (BOOL)validateWithStartTime:(NSString *)startTime withExpireTime:(NSString *)expireTime withTiem:(NSString *)time {
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"HH:mm"];
    
    NSDate *timeDate = [dateFormat dateFromString:time];
    
    NSDate *start = [dateFormat dateFromString:startTime];
    NSDate *expire = [dateFormat dateFromString:expireTime];
    
    if (([timeDate compare:start] == NSOrderedDescending && [timeDate compare:expire] == NSOrderedAscending) || [timeDate compare:start] == NSOrderedSame || [timeDate compare:expire] == NSOrderedSame) {
        return YES;
    }
    return NO;
}

    
@end
